package com.moyu.crowdsource.entity;

import com.moyu.crowdsource.validator.MyConstraint;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.social.security.SocialUserDetails;

import javax.validation.constraints.*;
import java.util.Collection;
import java.util.Date;

public class AccountUser implements SocialUserDetails {

  @ApiModelProperty(value = "用户ID(无需填写)")
  @Null
  private String userId;

  @ApiModelProperty(value = "昵称（注册时必填）")
  @NotBlank
  @Length(min =2,max = 8)
  private String nickname;

  @ApiModelProperty(value = "真实姓名（注册时必填）")
  @NotBlank
  @Pattern(regexp = "([\\u4e00-\\u9fa5]{2,6})", message = "名字必须为汉字且2-6")
  private String realname;

  @ApiModelProperty(value = "用户账户（注册时必填）")
  @NotBlank
  @Pattern(regexp = "^[a-zA-Z0-9_]{6,32}$", message = "用户名必须为数字或字母及其组合6-32")
  @MyConstraint
  private String accountUsername;

  @ApiModelProperty(value = "密码（注册时必填）")
  @NotBlank
  @Length(min =6,max = 32)
  private String accountPassword;

  @ApiModelProperty(value = "地址（注册时必填）")
  @NotBlank
  private String address;

  @ApiModelProperty(value = "头像地址(选填)")
  private String iconUrl;

  @ApiModelProperty(value = "学校地址（注册时必填）")
  @NotBlank
  private String school;

  @ApiModelProperty(value = "金钱(无需填写)")
  private String paymoney;

  @ApiModelProperty(value = "注册时间(无需填写)", readOnly = true)
  private Date registerTime;

  @ApiModelProperty(value = "最后登录时间(选填)")
  private Date lastlogin;

  @ApiModelProperty(value = "等级(无需填写)", readOnly = true)
  private String permission;

  @ApiModelProperty(value = "电话号码（注册时必填）")
  @NotBlank
  @Pattern(regexp = "^1[3|4|5|8][0-9]\\d{8}$", message = "手机号应为11位且为可用号码")
  private String phoneNumber;

  @ApiModelProperty(value = "性别(默认为男)")
  private String sex;

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex == null ? null : sex.trim();
  }

  public String getUserId() {
    return userId;
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }

  public String getNickname() {
    return nickname;
  }

  public void setNickname(String nickname) {
    this.nickname = nickname == null ? null : nickname.trim();
  }

  public String getRealname() {
    return realname;
  }

  public void setRealname(String realname) {
    this.realname = realname == null ? null : realname.trim();
  }

  public String getAccountUsername() {
    return accountUsername;
  }

  public void setAccountUsername(String accountUsername) {
    this.accountUsername = accountUsername == null ? null : accountUsername.trim();
  }

  public String getAccountPassword() {
    return accountPassword;
  }

  public void setAccountPassword(String accountPassword) {
    this.accountPassword = accountPassword == null ? null : accountPassword.trim();
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address == null ? null : address.trim();
  }

  public String getIconUrl() {
    return iconUrl;
  }

  public void setIconUrl(String iconUrl) {
    this.iconUrl = iconUrl == null ? null : iconUrl.trim();
  }

  public String getSchool() {
    return school;
  }

  public void setSchool(String school) {
    this.school = school == null ? null : school.trim();
  }

  public String getPaymoney() {
    return paymoney;
  }

  public void setPaymoney(String paymoney) {
    this.paymoney = paymoney == null ? null : paymoney.trim();
  }

  public Date getRegisterTime() {
    return registerTime;
  }

  public void setRegisterTime(Date registerTime) {
    this.registerTime = registerTime;
  }

  public Date getLastlogin() {
    return lastlogin;
  }

  public void setLastlogin(Date lastlogin) {
    this.lastlogin = lastlogin;
  }

  public String getPermission() {
    return permission;
  }

  public void setPermission(String permission) {
    this.permission = permission == null ? null : permission.trim();
  }

  public String getPhoneNumber() {
    return phoneNumber;
  }

  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    // return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN");
    return AuthorityUtils.commaSeparatedStringToAuthorityList(permission);
  }

  @Override
  public String getPassword() {
    return accountPassword;
  }

  @Override
  public String getUsername() {
    return accountUsername;
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return true;
  }

  @Override
  public String toString() {
    return this.accountUsername;
  }

  @Override
  public int hashCode() {
    return accountUsername.hashCode();
  }

  @Override
  public boolean equals(Object obj) {
    return this.toString().equals(obj.toString());
  }
}
